{extend name="public/base"/}
{block name="style"}
{/block}
{block name="content"}
    <div class="data-list" data-url="{:url('index')}">
        <form class="layui-form inline-form">
            <div class="pull-left">
                <div class="layui-inline">
                    <input class="layui-input input-order-sn" autofocus id="input-sn-type" autocomplete="off" name="sn" placeholder="订单号/追踪号">
                </div>
                <div class="layui-inline">
                    <input class="layui-input" autocomplete="off" name="name" placeholder="店铺或账号">
                </div>
                <div class="layui-inline">
                    <input class="layui-input laydate-range" autocomplete="off" name="date" placeholder="申报时间">
                </div>
                <div class="layui-inline">
                    <select name="track_status" lay-search lay-filter="data-list">
                        <option value="">物流状态</option>
                        {foreach $track_status as $k=>$v}
                        <option value="{$k}">{$v}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <select name="country_code" lay-search lay-filter="data-list">
                        <option value="">国家</option>
                        {foreach $counties as $c}
                        <option value="{$c.code_two}">{$c.name_ch}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <select name="channel_id" lay-search lay-filter="data-list">
                        <option value="">渠道</option>
                        {foreach $channel as $c}
                        <option value="{$c.channel_id}">{$c.channel_name}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <select name="platform_id" lay-search lay-filter="data-list">
                        <option value="">平台</option>
                        {foreach $platform as $p}
                        <option value="{$p.platform_id}">{$p.name}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm layui-btn-normal search"><i class="layui-icon layui-icon-search"></i></button>
                </div>
            </div>
        </form>
        <div class="layui-row">
            <div class="layui-inline layui-btn-group">
                <a id="batch-sync-info" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-refresh"></i>拉取物流信息</a>
                {if $is_show_sync_info}
                <a id="batch-sync-order" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-refresh"></i>同步物流到订单</a>
                {/if}
                {if $is_show_label}
                <a id="batch-download-label" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-download-circle"></i>批量获取面单</a>
                {/if}
            </div>
        </div>
        <div class="data">
            <p><i class="fa fa-spinner fa-spin"></i> 加载中...</p>
        </div>
    </div>
{/block}
{block name="script"}
<script type="text/javascript">
    layui.use(['layer', 'jquery', 'form'], function()
    {
        var layer = layui.layer
            , form = layui.form
            , $ = layui.jquery;
        // 批量同步物流信息
        $("#batch-sync-info").click(function()
        {
            var ids = getIds(".channel-order-id");
            if(ids.length < 1)
            {
                layer.msg("请选择要同步的订单", {icon: 5, time: 1500});
                return false;
            }
            layer.load(0, {shade: false});
            $.get("{:url('track')}", {ids: ids.join(',')}).done(function(response)
            {
                layer.closeAll('loading');
                var icon = 5, time = 1500;
                if(response.code == 1)
                {
                    $(".data").closest('.data-list').getList();
                    icon = 6;
                    time = 3000;
                }
                layer.msg(response.msg, {icon: icon, time: time});
            });
        });

        // 批量同步物流状态到订单
        $("#batch-sync-order").click(function()
        {
            var ids = getIds(".channel-order-id");
            if(ids.length < 1)
            {
                layer.msg("请选择要同步的订单", {icon: 5, time: 1500});
                return false;
            }
            layer.load(0, {shade: false});
            $.get("{:url('order')}", {ids: ids.join(',')}).done(function(response)
            {
                layer.closeAll('loading');
                var icon = 5, time = 1500;
                if(response.code == 1)
                {
                    $(".data").closest('.data-list').getList();
                    icon = 6;
                    time = 3000;
                }
                layer.msg(response.msg, {icon: icon, time: time});
            });
        });

        // 批量获取面单
        $("#batch-download-label").click(function()
        {
            var ids = getIds(".channel-order-id");
            if(ids.length < 1)
            {
                layer.msg("请选择要下载面单的订单", {icon: 5, time: 1500});
                return false;
            }
            layer.load(0, {shade: false});
            $.get("{:url('label')}", {ids: ids.join(',')}).done(function(response)
            {
                layer.closeAll('loading');
                var icon = 5, time = 1500;
                if(response.code == 1)
                {
                    $(".data").closest('.data-list').getList();
                    icon = 6;
                    time = 3000;
                }
                layer.msg(response.msg, {icon: icon, time: time});
            });
        });

        // 置空单号输入框
        $("#input-sn-type").keydown(function(event)
        {
            if(event.keyCode == 13)
            {
                var _that = $(this);
                setTimeout(() => {_that.val('')}, 100);
            }
        });

        // 切换单号类型
        form.on('select(order-type)', function(data)
        {
            $("#input-sn-type").attr('name', data.value).attr('placeholder', $(data.elem).find("option:selected").text());
        });
    });
</script>
{/block}